#include<stdio.h>
#include<conio.h>
typedef struct room{
    long start, end,index;
    room *left,*right,*up;
};
room *list=NULL;
long i,n,arr[100066],count=0,id=0;

room *newRoom(long start,long end,long index,room *left,room *right,room *up){
    room *r=new room;
    (*r).start=start;
    (*r).end=end;
    (*r).index=index;
    (*r).left=left;
    (*r).right=right;
    (*r).up=up;
    return r;
}
void insert(long start,long end,long index){
    room *first=list,*last=list;
    while(first!=NULL){
        last=first;
        first=((*first).start<start)?(*first).right:(*first).left;
    }
    if(last==NULL){
        list=newRoom(start,end,index,NULL,NULL,NULL);
        return;
    }
    if((*last).start<start)(*last).right=newRoom(start,end,index,NULL,NULL,last);
    else (*last).left=newRoom(start,end,index,NULL,NULL,last);
}
long deleteRoom(long start){
    room *accept=NULL,*p=list;
    long retval=0;
    /* Tim kiem cho co the xoa */
    while(p!=NULL){
        if((*p).start>=start)accept=p;
        if((*p).start==start)break;
        p=((*p).start>start)?(*p).left:(*p).right;
    }
   
    /* Neu tim khong thay thi thoat di*/
    if(accept==NULL)return 0;
    arr[++id]=(*accept).index;
    retval=(*accept).end;
    /* Neu co 2 nhanh */
    if((*accept).left!=NULL && (*accept).right!=NULL){
        p=(*accept).left;
        while((*p).right!=NULL)p=(*p).right;
        
        (*accept).start=(*p).start;
        (*accept).end=(*p).end;
        (*accept).index=(*p).index;
        
        if((*accept).left==p)(*accept).left=NULL;
        else (*(*p).up).right=NULL;
        delete p;
        return retval;
    }
    p=(*accept).up;
    /* Neu khong co nhanh nao*/
    if(((*accept).left==NULL) and ((*accept).right==NULL)){
        if(accept==list){
            list=NULL;
            return retval;
        }
        if(accept==(*p).left)(*p).left=NULL;
        else (*(*accept).up).right=NULL;
        delete accept;
        return retval;
    }
    /* Neu chi co mot nhanh*/
    if((*accept).left!=NULL){/* Co nhanh left*/
        if(list==accept){
            list=(*accept).left;
            (*(*accept).left).up=NULL;
        }else if((*p).left==accept){
            (*p).left=(*accept).left;
            (*(*accept).left).up=p;
        }else{
            (*p).right=(*accept).left;
            (*(*accept).left).up=p;
        }
    }else{/* Co nhanh right */
        if(list==accept){
            list=(*accept).right;
            (*(*accept).right).up=NULL;
        }else if((*p).left==accept){
            (*p).left=(*accept).right;
            (*(*accept).right).up=p;
        }else{
            (*p).right=(*accept).right;
            (*(*accept).right).up=p;
        }
    }
    delete accept;
    return retval;
}   
void display(room *l){
    if(l==NULL)return;
    display((*l).left);
    printf("%ld ",(*l).start);
    display((*l).right);
}
void input(){
    long u,v;
    FILE *fp=fopen("ROOMS.INP","r");
    fscanf(fp,"%ld",&n);
    for(i=0;i<n;i++){
        fscanf(fp,"%ld%ld",&u,&v);
        insert(u,v,i+1);
    }
    fclose(fp);
}
void process(){
    room *p;
    long k=0;
    arr[0]=-1;
    while(list!=NULL){
        do 
            k=deleteRoom(k);
        while(k>0);
        arr[++id]=-1;
        count++;
    };
}
void output(){
    FILE *fp=fopen("ROOMS.OUT","w");
    fprintf(fp,"%ld",count);
    for(long i=0;i<id;i++)
        if(arr[i]==-1)fprintf(fp,"\n");
        else{
            if(i>0 && arr[i-1]>0)fprintf(fp," ");
            fprintf(fp,"%ld",arr[i]);
        }
    fclose(fp);
}
int main(){
    input();
    process();
    display(list);
    output();
    return 0;
}
